const path = require('path')
const os = require('os')
const {BannerPlugin} = require('webpack')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const {defineConfig} = require('@vue/cli-service')

module.exports = defineConfig({
  transpileDependencies: ['*'],
  lintOnSave: false,
  runtimeCompiler: true,
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
  productionSourceMap: false,
  // outputDir: 'dist',
  outputDir: path.resolve(__dirname, `dist`),
  devServer: {
    port: 9000,
    open: false,
    proxy: {}
  },
  chainWebpack: config => {
    config.resolve.alias.set('@', path.resolve(__dirname, `src`))
    config.resolve.alias.set('@lib', path.resolve(__dirname, `lib`))
  },
  configureWebpack: (config) => {
    config.optimization.minimizer.push(
      new UglifyJsPlugin({
        uglifyOptions: {
          compress: {
            drop_console: true, // console
            drop_debugger: false,
            pure_funcs: ['console.log'] // 移除console
          }
        }
      })
    )
    if (process.env.NODE_ENV === 'production') {
      const rule = /app\.[\w]+\.js$/
      // 追加打包人信息-配置参考：https://doc.codingdict.com/webpack-cn-doc/plugins/banner-plugin/
      config.plugins.push(
        new BannerPlugin({
          raw: true,
          entryOnly: true,
          include: rule,
          banner: `console.log('build: ${os.hostname()} ${new Date().toLocaleString()}');`
        })
      )
    }
  },
  css: {
    loaderOptions: {
      scss: {
        additionalData: ``,
      }
    }
  }
})
